package com.ym.controller.admin;

import com.github.pagehelper.PageInfo;
import com.ym.common.utils.page.PageDomain;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.medical.domain.entity.SysCodeRuleEntity;
import com.ym.medical.domain.param.SysCodeRuleQueryParam;
import com.ym.medical.domain.param.SysCodeRuleUpdateParam;
import com.ym.medical.domain.vo.SysCodeRuleListPcVO;
import com.ym.medical.service.ISysCodeRuleSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

import static com.ym.common.constant.ValidRule.Add;
import static com.ym.common.constant.ValidRule.Update;

/**
 * 编码生产Controller
 *
 * @author ym
 * @date 2020-08-17
 */
@Api(tags = "admin-编码生产")
@RestController
@Validated
@RequestMapping("/sys/rule")
public class SysCodeRuleController extends BaseController {

	@Autowired
	private ISysCodeRuleSV sysCodeRuleSV;

	@ApiOperation("查询编码生产列表")
	@Log(dataBase = false, printBody = false, printQuery = true)
	@GetMapping("/list")
	public APIResponse<PageInfo<SysCodeRuleListPcVO>> list(@Valid SysCodeRuleQueryParam queryParam) {
		PageDomain.startPage();
		List<SysCodeRuleListPcVO> list = sysCodeRuleSV.selectListPC(queryParam);
		return successPage(list);
	}

	@ApiOperation("获取编码生产详细信息")
	@Log(dataBase = false, printBody = false, printQuery = true)
	@GetMapping(value = "/info")
	public APIResponse<SysCodeRuleEntity> info(@RequestParam Long id) {
		SysCodeRuleEntity data = sysCodeRuleSV.selectById(id);
		return successData(data);
	}

	@ApiOperation("新增编码生产")
	@Log(printQuery = false)
	@PostMapping("/insert")
	public APIResponse insert(@RequestBody @Validated(value = Add.class) SysCodeRuleUpdateParam updateParam) {
		Long id = sysCodeRuleSV.insertPC(updateParam);
		return successData(id);
	}

	@ApiOperation("修改编码生产")
	@Log(printQuery = false)
	@PostMapping("/update")
	public APIResponse update(@RequestBody @Validated(value = Update.class) SysCodeRuleUpdateParam updateParam) {
		int result = sysCodeRuleSV.updateByIdPC(updateParam);
		return successData(result);
	}

	@ApiOperation("删除编码生产")
	@Log(printQuery = false)
	@PostMapping("/delete")
	public APIResponse delete(@RequestBody @Valid @NotNull Long[] ids) {
		int result = sysCodeRuleSV.deleteByIds(ids);
		return successData(result);
	}
}
